/*  
    Copyright 2010 Mike Eheler <mike.eheler@gmail.com>

    This file is part of Four Falling Blocks Puzzle Game.

    Four Falling Blocks Puzzle Game is free software: you can
	redistribute it and/or modify it under the terms of the GNU General
	Public License as published by the Free Software Foundation, either
	version 3 of the License, or (at your option) any later version.

    Four Falling Blocks Puzzle Game is distributed in the hope that it
	will be useful, but WITHOUT ANY WARRANTY; without even the implied
	warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Four Falling Blocks Puzzle Game.
	
	If not, see <http://www.gnu.org/licenses/>.
*/

#include "debug_console.h"
#include <QtGui/QTextEdit>

DebugConsole::DebugConsole()
{
	mWindow = new QWidget( 0, Qt::WindowMinMaxButtonsHint );
	mWindow->setWindowTitle( "Debug Console" );

	QHBoxLayout * win_layout = new QHBoxLayout( mWindow );
	win_layout->setMargin( 0 );
	win_layout->setSpacing( 0 );

	mOutput = new QTextEdit( mWindow );
	mOutput->setReadOnly( true );
	mOutput->setAcceptRichText( false );
	mOutput->setFrameStyle( QFrame::NoFrame );
	
	QFont output_font( mOutput->font() );
	output_font.setFamily( "Courier New" );
	output_font.setPointSize( 11 );

	QPalette output_palette( mOutput->palette() );
	output_palette.setColor( QPalette::Base, QColor(Qt::black) );
	output_palette.setColor( QPalette::Text, QColor(191,191,191) );

	mOutput->setFont( output_font );
	mOutput->setPalette( output_palette );

	win_layout->addWidget( mOutput );

	print( "Debug Console\n" );

	mWindow->move( 0, 0 );
	mWindow->resize( 640, 480 );
	mWindow->show();
}

DebugConsole::~DebugConsole()
{
	if( mOutput )
	{
		mOutput->deleteLater();
		mOutput = 0;
	}
	
	if( mWindow )
	{
		mWindow->deleteLater();
		mWindow = 0;
	}
}

DebugConsole & DebugConsole::instance()
{
	static DebugConsole console;
	return console;
}

// HACK :(
QWidget * DebugConsole::widget()
{
	return instance().mWindow;
}

void DebugConsole::print( char const * msg )
{
	if( instance().mOutput )
		instance().mOutput->append( QString(msg) );
}

void DebugConsole::error( char const * msg )
{
	print( qPrintable(QString("Error: ") + msg) );
}

void DebugConsole::warning( char const * msg )
{
	print( qPrintable(QString("Warning: ") + msg) );
}

void DebugConsole::info( char const * msg )
{
	print( qPrintable(QString("Info: ") + msg) );
}
